﻿require 'watir-webdriver'
require 'win32ole'

module ZDDI

    module DNS
        def self.open_dns_page
            sleep 0.5
            ZDDI.browser.link(:class, "dns").click
            sleep 1
        end
        def self.open_share_rr_page
            open_dns_page
            ZDDI.browser.link(:class, "shared-rrs").click
            sleep 2
        end
        def self.open_search_page
            open_dns_page
            ZDDI.browser.link(:class, "search").click
            sleep 2
        end
        def self.open_acl_page
            open_dns_page
            ZDDI.browser.link(:class, "acls").click
            sleep 2
        end
        def self.open_selected_elem_page
            ZDDI.browser.div(:id, "mainTable").table(:index, 0).link(:class, "refs").click
            waiting_operate_finished
        end
        def self.open_view_page
            open_selected_elem_page
        end
        def self.open_zone_page
            open_selected_elem_page
        end
        def self.open_domain_page
            open_selected_elem_page
        end
        def self.popup_right_menu(operation="create", selected=false)
            #if operation != "batchCreate"
            #    ZDDI.browser.div(:id, "content").div(:id, "mainTable").right_click
            #else
            #    ZDDI.browser.div(:id, "content").div(:id, "mainTable").table(:index, 0).rows[0].right_click
            #end
            #sleep 1
            #ZDDI.browser.span(:class, operation).click
            if selected
                ZDDI.browser.div(:id, "mainTable").checkbox(:class, "checkAll").set
            end
            ZDDI.browser.div(:id, "toolsBar").button(:class, operation).click
            sleep 1
        end
        def self.del_current_elem(id="ok")
            #popup_right_menu("del")
            ZDDI.browser.div(:id, "mainTable").checkbox(:class, "checkAll").set
            ZDDI.browser.div(:id, "toolsBar").button(:class, "del").click
            sleep 1
            ZDDI.browser.div(:id, "popWin").button(:class, id).click
            waiting_operate_finished
        end
        def self.elem_exists?(elem_name)
            ZDDI.browser.div(:id, "search").text_field(:class, "search").set(elem_name)
            ZDDI.browser.div(:id, "search").button(:class, "searchBut").click
            result = get_cur_elem_string.to_s.include?(elem_name)
            return result
        end
        def self.view_exists?(view_name)
            return elem_exists?(view_name)
        end
        def self.zone_exists?(zone_name)
            return elem_exists?(zone_name)
        end
        def self.domain_exists?(domain_name)
            return elem_exists?(domain_name)
        end
        def self.acl_exists?(acl_name)
            return elem_exists?(acl_name)
        end
        def self.share_rr_exists?(share_rr_name)
            return elem_exists?(share_rr_name)
        end
        def self.search_exists?(name)
            return elem_exists?(name)
        end
        def self.get_elem_num
            return get_cur_elem_string.length - 1
        end
        def self.get_cur_elem_string(name="NULL")
            if name != "NULL"
                ZDDI.browser.div(:id, "search").text_field(:class, "search").set(name)
            end
            ZDDI.browser.div(:id, "search").button(:class, "searchBut").click
            content = ZDDI.browser.div(:id, "mainTable").table(:index, 0).strings
            if !content
               content = get_cur_elem_string(name)
            end
            return content
        end
        def self.waiting_operate_finished
            while ZDDI.browser.element(:id, "masker").present?
                sleep 1
            end
            sleep 1
        end
        def self.send_enter
            ZDDI.browser.send_keys("\r\n")
        end
        def self.send_newline
            ZDDI.browser.send_keys("\n")
        end
        def self.open_dialog(file_path)
            autoit = WIN32OLE.new('AutoItX3.Control')    #init autoit            
            autoit.WinWaitActive("打开", "", 9)    #acitve choose file window
            sleep 1
            autoit.ControlSetText("打开", "", "Edit1", file_path)    #input zone file path
            sleep 1
            autoit.ControlClick("打开", "", "Button2")    #click open button
            sleep 1                
        end
        def self.goto_specify_page(view_name, zone_name=nil)
            r = false
            open_dns_page
            if view_exists?(view_name)
                open_view_page
                r = true
                if (zone_name && zone_exists?(zone_name))
                    open_zone_page
                elsif (zone_name && !zone_exists?(zone_name))
                    r = false
                end
            else
                r = false 
            end
            return r
        end
        def self.add_rr(domain, set_name=true)
            if set_name
                ZDDI.browser.div(:id, "popWin").text_field(:name, "name").set(domain["rname"])
            end
            ZDDI.browser.div(:id, "popWin").select(:name, "type").select(domain["rtype"])
            sleep 1
            ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(domain["ttl"])
            ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(domain["rdata"])
            sleep 1
            ZDDI.browser.div(:id, "popWin").button(:class, "save").click
            DNS.waiting_operate_finished
        end
        class ACL
            def create_acl(args)
                @acl_name = args[:acl_name]
                @acl_list = args[:acl_list]
                @file_path = args[:filename]

                DNS.open_acl_page
                DNS.popup_right_menu

                ZDDI.browser.div(:id, "popWin").text_field(:name, "acl_name").set(@acl_name)
                if @file_path
                    ZDDI.browser.div(:id, "popWin").file_field(:id, "networksFile").click
                    DNS.open_dialog(@file_path)
                else
                    @acl_list.each { |acl|
                        ZDDI.browser.div(:id, "popWin").textarea(:name, "networks").append(acl)
                        DNS.send_newline
                    }
                end
                ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                DNS.waiting_operate_finished
                r = ""
                if DNS.acl_exists?(@acl_name)
                    r += "succeed to create acl: #{@acl_name}\n"
                else
                    r += "failed to create acl: #{@acl_name}\n"
                end
                puts r
                return r
            end
            def edit_acl(args)
                r = ""
                @acl_name = args[:acl_name]
                @del_ip = args[:del_ip]
                @add_ip = args[:add_ip]
                DNS.open_acl_page
                if DNS.acl_exists?(@acl_name)
                    DNS.popup_right_menu("edit", true)
                    acl_list = ZDDI.browser.div(:id, "popWin").textarea(:name, "networks").value.split("\n")
                    acl_list -= @del_ip
                    acl_list += @add_ip
                    ZDDI.browser.div(:id, "popWin").textarea(:name, "networks").set(acl_list.join("\n"))
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                    content = DNS.get_cur_elem_string
                    puts content[1][2]
                    puts acl_list.join(" ")
                    acl_list.each {|acl|
                        if !content[1][2].include?(acl)
                            r += "failed to edit acl: #{@acl_name}\n"
                        end
                    }
                    if !r.match("failed")
                        r += "succeed to edit acl: #{@acl_name}\n"
                    end
                else
                    r += "not find acl: #{@acl_name}\n"
                end
                puts r
                return r
            end
            def del_acl(acl_list)
                r = ""
                DNS.open_acl_page
                acl_list.each { |acl_name|
                    if DNS.acl_exists?(acl_name)
                        DNS.del_current_elem("ok")
                        r += "succeed to del acl: #{acl_name}\n"
                    else
                        r += "not find acl: #{acl_name}\n"
                    end
                }
                puts r
                return r
            end
        end
        class VIEW
            def create_view(args)
                @view_name = args[:view_name]
                @acl_list= args[:acl_list]
                @dns64_list= args[:dns64_list]
                @owner_list = args[:owner_list]
                DNS.open_dns_page
                DNS.popup_right_menu

                ZDDI.browser.div(:id, "popWin").text_field(:name, "view_name").set(@view_name)
                @owner_list.each { |owner|
                    ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                    DNS.send_enter
                }
                @acl_list.each { |acl|
                    ZDDI.browser.div(:id, "popWin").text_field(:value, "选择访问控制").set(acl)
                    DNS.send_enter
                }
                @dns64_list.each {|dns64|
                    ZDDI.browser.div(:id, "popWin").textarea(:name, "dns64s").append(dns64)
                    DNS.send_newline
                }
                ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                DNS.waiting_operate_finished

                return check_view
            end
            
            def edit_view(args)
                @view_name = args[:view_name]
                @add_acl = args[:add_acl]
                @del_acl = args[:del_acl]
                @dns64_list= args[:dns64_list]
                DNS.open_dns_page
                if DNS.view_exists?(@view_name) then
                    DNS.popup_right_menu("edit", true)
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "dns64s").clear
                    @dns64_list.each { |dns64|
                        ZDDI.browser.div(:id, "popWin").textarea(:name, "dns64s").append(dns64)
                        DNS.send_newline
                    }
                    @add_acl.each {|acl|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择访问控制").set(acl)
                        DNS.send_enter
                    }
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                end
                return check_view("edit")
            end
            def check_view(operation="create")
                r = ""
                context = DNS.get_cur_elem_string(@view_name)
                if operation == "edit" && context[1][5].include?(@add_acl.join(' ')) && context[1][6].include?(@dns64_list.join(' ').downcase)
                    r += "succeed"
                elsif operation != "edit" && context[1][2].include?(@owner_list.join('. '))
                    r += "succeed"
                else
                    r += "failed"
                end
                r += " to #{operation} view: #{@view_name}\n"
                puts r
                return r
            end
            def modify_view(args)
                DNS.open_dns_page
                @view_name = args[:view_name]
                @owner_list = args[:owner_list]
                if DNS.view_exists?(@view_name)
                    DNS.popup_right_menu("modifyByMembers", true)
                    @owner_list.each { |owner|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                        DNS.send_enter
                    }
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                end
                return check_view("modify")
            end
            def del_view(view_list)
                r = ""
                DNS.open_dns_page
                view_list.each { |view_name|
                    if DNS.view_exists?(view_name)
                        DNS.del_current_elem
                    end
                }
                view_list.each { |view_name|
                    if !DNS.view_exists?(view_name)
                        r += "succeed to del view: #{view_name}\n"
                    else
                        r += "failed to del view: #{view_name}\n"
                    end
                }
                puts r
                return r
            end
        end

        class ZONE
            def create_zone(args)
                @view_name = args[:view_name]
                @zone_name = args[:zone_name]
                @owner_list = args[:owner]
                r = ""
                if DNS.goto_specify_page(@view_name)
                    DNS.popup_right_menu
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "zone_name").set(@zone_name)
                    sleep 1
                    @owner_list.each { |owner|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                        DNS.send_enter
                    }

                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished

                else
                    r += "no find view: #{@view_name}"
                end
                r += check_zone("create")
                return r
            end
            def create_zone_from_file(args)
                @view_name = args[:view_name]
                @zone_name = args[:zone_name]
                @zone_file = args[:zone_file]
                @owner_list = args[:owner]
                r = ""
                if DNS.goto_specify_page(@view_name)
                    DNS.popup_right_menu
                    ZDDI.browser.div(:id, "popWin").radio(:value, "zone_file").set
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "zone_name").set(@zone_name)
                    sleep 1
                    @owner_list.each { |owner|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                        DNS.send_enter
                    }
                    ZDDI.browser.div(:id, "popWin").file_field(:name, "zone_file").click
                    sleep 1
                    DNS.open_dialog(@zone_file)
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                else
                    r += "no find view: #{@view_name}"
                end
                r += check_zone("create_from_file")
                return r
            end
            def create_zone_from_copy(args)
                @src_view = args[:src_view]
                @view_name = args[:dst_view]
                @zone_name = args[:zone_name]
                @owner_list = args[:owner]
                r = ""
                if DNS.goto_specify_page(@view_name)
                    DNS.popup_right_menu
                    ZDDI.browser.div(:id, "popWin").radio(:value, "zone_copy").set
                    sleep 1
                    @owner_list.each { |owner|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                        DNS.send_enter
                    }
                    ZDDI.browser.div(:id, "popWin").select(:name, "view_and_zone").select(@zone_name)
                    sleep 1
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                else
                    r += puts "not find view: #{@view_name}"
                end
                r += check_zone("create_from_copy")
                return r
            end
            def check_zone(operation="create")
                r = ""
                context = DNS.get_cur_elem_string(@zone_name)
                puts "get:  #{context[1][2]}"
                puts "set:  #{@owner_list.join(' ')}"
                if context[1][2].include?(@owner_list.join('. ')) && context[1][3].to_i != 0
                    r += "succeed to #{operation} zone: #{@zone_name} in view: #{@view_name} owner_list: #{@owner_list}\n"
                else
                    @owner_list.each {|owner|
                        if !context[1][2].include?(owner)
                           r += "failed to #{operation} zone: #{@zone_name} in view: #{@view_name} in device: #{owner}\n"
                        end
                    }
                end
                puts r
                return r
            end
            def modify_zone(args)
                r = ""
                DNS.open_dns_page
                @view_name = args[:view_name]
                @zone_name = args[:zone_name]
                @owner_list = args[:owner_list]
                if DNS.goto_specify_page(@view_name)
                    if DNS.zone_exists?(@zone_name)
                        DNS.popup_right_menu("modifyByMembers", true)
                        @owner_list.each { |owner|
                            ZDDI.browser.div(:id, "popWin").text_field(:value, "选择设备节点").set(owner)
                            DNS.send_enter
                        }
                        ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                        DNS.waiting_operate_finished
                    else
                        r += "not find zone: #{@zone_name} in view: #{@view_name}"
                    end
                else
                    r += "no find view: #{@view_name}\n"
                end
                r += check_zone("modify")
                return r
            end
            def delete_zone(args)
                view_name = args[:view_name] 
                zone_list = args[:zone_list] 
                if DNS.goto_specify_page(view_name)
                    zone_list.each { |zone_name|
                        if DNS.zone_exists?(zone_name)
                            DNS.del_current_elem
                        end
                    }
                end
                r = ""
                zone_list.each { |zone_name|
                    if !DNS.zone_exists?(zone_name)
                        r += "succeed to del zone: #{zone_name} in view: #{view_name}\n"
                    else
                        r += "failed to del zone: #{zone_name}\n"
                    end
                }
                puts r
                return r
            end
        end
        class DOMAIN 
            def self.check_domain(view_name, zone_name, domain_list, operation='create')
                r = ""
                if DNS.goto_specify_page(view_name, zone_name)
                    domain_list.each {|domain_name|
                        d_name = domain_name["rname"] + '.' + zone_name
                        if operation != "delete" and DNS.domain_exists?(d_name)
                            context = DNS.get_cur_elem_string
                            puts context[1][3]
                            puts context[1][4]
                            puts domain_name['rdata']
                            if context[1][3] == domain_name['ttl'] and context[1][4].include?(domain_name['rdata'])
                                r += "succeed to #{operation} "
                            else
                                r += "failed to #{operation} "
                            end
                        elsif operation == "delete" and !DNS.domain_exists?(d_name)
                            r += "succeed to delete"
                        else
                            r += "failed to #{operation} "
                        end
                        r += " domain: #{d_name} in view: #{view_name}, rr_info: #{domain_name}\n"
                    }
                end
                puts r
                return r
            end
            def create_domain(args)
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                domain_list = args[:domain_list]
                if DNS.goto_specify_page(view_name, zone_name)
                    domain_list.each { |domain|
                        DNS.popup_right_menu
                        DNS.add_rr(domain)
                    }
                end
                return DOMAIN.check_domain(view_name, zone_name, domain_list)
            end
            def create_batch_domain(args)
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                file_name = args[:file_name]
                example_domain = args[:example_domain]
                if DNS.goto_specify_page(view_name, zone_name)
                    DNS.popup_right_menu("batchCreate")
                    ZDDI.browser.div(:id, "popWin").file_field(:name, "zone_file").click
                    DNS.open_dialog(file_name)
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                    sleep 1
                end
                example_domain.each { |domain_name|
                    if !DNS.goto_specify_page(view_name, zone_name) or !DNS.domain_exists?(domain_name)
                        r = "failed to create batch domain in zone: #{zone_name}, in view: #{view_name}\n"
                        puts r
                        return r
                    end
                }
                r = "succeed to create batch domain in zone: #{zone_name}, in view: #{view_name}\n"
                puts r
                return r
            end
            def edit_domain(args)
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                domain_list = args[:domain_list]
                if DNS.goto_specify_page(view_name, zone_name)
                    domain_list.each { |domain|
                        if DNS.domain_exists?(domain["rname"] + '.' + zone_name)
                            DNS.popup_right_menu("edit", selected=true)
                            ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(domain["ttl"])
                            ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(domain["rdata"])
                            ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                            DNS.waiting_operate_finished
                        end
                    }
                end
                return DOMAIN.check_domain(view_name, zone_name, domain_list, 'edit')
            end
            def edit_batch_domain(args)
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                domain_list = args[:domain_list]
                ttl = domain_list[0]["ttl"]
                rdata = domain_list[0]["rdata"]
                r = ""
                if DNS.goto_specify_page(view_name, zone_name)
                    ZDDI.browser.div(:id, "content").button(:class, "showMore").click
                    domain = domain_list[0]["rname"] + "." + zone_name
                    if DNS.domain_exists?(domain)
                        DNS.popup_right_menu("batchEdit", true)
                        ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(ttl)
                        ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(rdata)
                        ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                        DNS.waiting_operate_finished
                    else
                        r += "failed to find domain: #{domain} when batch edit\n"
                    end
                    r+= DOMAIN.check_domain(view_name, zone_name, domain_list, 'batchedit')
                    puts r
                    #domain_list.each { |domain|
                    #     domain = domain + "." + zone_name
                    #     if DNS.domain_exists?(domain)
                    #        content = DNS.get_cur_elem_string
                    #        if context[1][3] == ttl and context[1][4].include?(rdata)
                    #            r += "succeed to batch edit "
                    #        else
                    #            r += "failed to batch edit "
                    #        end
                    #     else
                    #        r += "failed to find domain: #{domain} when batch edit\n"
                    #     end
                    #}
                end
                return r
            end
            def del_domain(args)
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                domain_list = args[:domain_list]
                if DNS.goto_specify_page(view_name, zone_name)
                    domain_list.each { |domain|
                        if DNS.domain_exists?(domain)
                            DNS.del_current_elem
                        end
                    }
                end
                r = ""
                if DNS.goto_specify_page(view_name, zone_name)
                    domain_list.each { |domain|
                        if !DNS.domain_exists?(domain)
                            r += "succeed to del domain: #{domain}\n"
                        else
                            r += "failed to del domain: #{domain}\n"
                        end
                    }
                end
                puts r
                return r
            end
        end
        class SHARERR
            def create_share_rr(args)
                @domain_list = args[:domain_list]
                @share_owner = args[:share_owner]
                DNS.open_share_rr_page
                DNS.popup_right_menu
                @domain_list.each {|domain|
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "domain_name").set(domain["rname"])
                    ZDDI.browser.div(:id, "popWin").select(:name, "type").select(domain["rtype"])
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(domain["ttl"])
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(domain["rdata"])
                    sleep 1
                    @share_owner.each { |owner|
                        ZDDI.browser.div(:id, "popWin").text_field(:value, "选择共享区").set(owner)
                        DNS.send_enter
                    }
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                }
                return check_share_rr
            end
            def edit_share_rr(args)
                @domain_list = args[:domain_list]
                @share_owner = args[:share_owner]
                DNS.open_share_rr_page
                DNS.popup_right_menu("edit", true)
                @domain_list.each {|domain|
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(domain['ttl'])
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(domain['rdata'])
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                }
                return check_share_rr("edit")
            end
            def check_share_rr(operation="create")
                r = ""
                @share_owner.each {|owner|
                    view_name, zone_name = owner.split('.', 2)
                    result = DOMAIN.check_domain(view_name, zone_name, @domain_list, operation)
                    if result.match("succeed")
                        r += result
                        r += "succeed to #{operation}"
                    else
                        r += result
                        r += "failed to #{operation}"
                    end
                    result = ""
                    r += " share rr: #{@domain_list} in zone: #{zone_name} in view: #{view_name}\n"
                }
                puts r
                return r
            end
            def del_share_rr(args)
                @domain_list = args[:domain_list]
                @share_owner = args[:share_owner]
                DNS.open_share_rr_page
                @domain_list.each { |domain|
                   if DNS.share_rr_exists?(domain['rname'])
                       DNS.del_current_elem
                   end
                }
                return check_share_rr("delete")
            end
            def modify_share_rr(args)
                r = ""
                share_rr_list = args[:share_rr_list]
                add_owner = args[:add_owner]
                DNS.open_share_rr_page
                share_rr_list.each { |share_rr|
                    if DNS.share_rr_exists?(share_rr)
                        DNS.popup_right_menu("modifyByOwner", true)
                        add_owner.each {|owner|
                            ZDDI.browser.div(:id, "popWin").text_field(:value, "选择成员").set(owner)
                            DNS.send_enter
                        }
                        ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                        DNS.waiting_operate_finished
                        context = DNS.get_cur_elem_string
                        if context[1][5].include?(add_owner.join(" ")) 
                            r += "succeed " 
                        else
                            r += "failed "
                        end
                        r += " to modify owner of share_rr: #{share_rr} modify owner: #{add_owner}"

                    end
                }
                puts r
                return r
            end
        end
       class SEARCH
            def search_domain(args)
                DNS.open_search_page
                domain_name = args[:domain_name]
                domain_num = args[:doamin_num]
                if DNS.search_exists?(domain_name)
                    if DNS.get_elem_num == domain_num
                        r = "succeed to search domain: #{domain_name}, find num: #{domain_num}\n"
                    else
                        r = "failed to search domain: #{domain_name}, find num: #{DNS.get_elem_num}, actual num: #{domain_num}\n"
                    end
                else
                    r = "failed to search domain: #{domain_name}\n"
                end
                puts r
                return r
            end
            def search_edit_and_verify(search, ttl, rdata, operation="edit", type="domain")
                r = ""
                DNS.open_search_page
                if DNS.search_exists?(search)
                    DNS.popup_right_menu(operation, true)
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "ttl").set(ttl)
                    ZDDI.browser.div(:id, "popWin").text_field(:name, "rdata").set(rdata)
                    ZDDI.browser.div(:id, "popWin").button(:class, "save").click
                    DNS.waiting_operate_finished
                    if type == "ip"
                        DNS.open_search_page
                        if !DNS.search_exists?(rdata)
                            r += "failed "
                        end
                    end
                    context = DNS.get_cur_elem_string
                    if context[1][6] == ttl and context[1][7] == rdata
                        r += "succeed "
                    else
                        r += "failed "
                    end
                    r += " to #{operation} search: #{search} in search block, rdata: #{rdata}, ttl: #{ttl}\n"
                end
                puts r
                return r
            end
            def search_domain_edit(args)
                r = ""
                DNS.open_search_page
                domain_name = args[:domain_name]
                ttl = args[:ttl]
                rdata = args[:rdata]
                return search_edit_and_verify(domain_name, ttl, rdata, "edit")
            end
            def search_domain_batch_edit(args)
                DNS.open_search_page
                domain_name = args[:domain_name]
                rdata = args[:rdata]
                ttl = args[:ttl]
                return search_edit_and_verify(domain_name, ttl, rdata, "batchEdit")
            end
            def search_ip(args)
                DNS.open_search_page
                ip = args[:ip]
                ip_num = args[:ip_num]
                if DNS.search_exists?(ip)
                    if DNS.get_elem_num == ip_num
                        r = "succeed to search ip: #{ip}, find num: #{ip_num}\n"
                    else
                        r = "failed to search ip: #{ip}, find num: #{DNS.get_elem_num}, actual num: #{ip_num}\n"
                    end
                else
                    r = "failed to search ip: #{ip}\n"
                end
                puts r
                return r
            end
            def search_ip_batch_edit(args)
                ip = args[:ip]
                ttl = args[:ttl]
                rdata = args[:rdata]
                return search_edit_and_verify(ip, ttl, rdata, "batchEdit", "ip")
            end
	   end
       class SYNCDATA
            def sync_data(args)
                owner= args[:owner_list]
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                zone_file = args[:zone_file]
                zone = ZDDI::DNS::ZONE.new
                if not zone_file
                    zone.create_zone(args)
                else
                    zone.create_zone_from_file(args)
                end
            end
            def compare_zone_data(args)
                server_list = args[:server_list]
                domain_name = args[:domain_name]
                rtype = args[:rtype]
                port = args[:port]
                res1 = `dig @#{server_list[0]} -p #{port} #{domain_name} #{rtype}"`
                status1 = res1.match("status:.*,").to_s
                answer1 = res1.match("ANSWER SECTION:\n.*\n").to_s
                r = ""
                for i in 1..server_list.length-1 do
                    res2 =`dig @#{server_list[i]} -p #{port} #{domain_name} #{rtype}` 
                    status2 = res2.match("status:.*,").to_s
                    puts status1
                    puts status2
                    answer2 = res2.match("ANSWER SECTION:\n.*\n").to_s
                    puts answer1
                    puts answer2
                    if  status1 == status2 and answer1 == answer2
                        r += "succeed "
                    else
                        r += "failed " 
                    end
                    r += "to verify data sync in zone: #{domain_name} between server: #{server_list[0]} and #{server_list[i]}\n"
                end
                if server_list.length == 1
                    r += "succeed to verify\n"
                end
                puts r
                return r
            end
            def sync_data_by_stop_service(args)
                owner = args[:owner_list]
                view_name = args[:view_name]
                zone_name = args[:zone_name]
                ZDDI::CLOUD.open_cloud_page
                zone = ZDDI::DNS::ZONE.new
                zone.create_zone(args)
            end
       end
    end
end
